package snakegame;

/* SnakeGame V1.2
 * by FrankkieNL
 * OPEN SOURCE
 */

import gnu.getopt.Getopt;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.InputStream;
import java.util.Properties;
import javax.swing.JFrame;

public class Main {

  // <editor-fold defaultstate="collapsed" desc="public static ...">
  public static JFrame frame;
  public static final int SYSTEM_LOOK = 0;
  public static final int NIMBUS_LOOK = 1;
  public static final int METAL_LOOK = 2;
  public static int huidigeLook = SYSTEM_LOOK;
  public static String propertiesFile = "frame.properties";
  public static int frameW = 640;
  public static int frameH = 480;
  public static boolean frameResizable = true;
  public static boolean dingFrameZichtbaar = false;
  public static boolean debug = false;
  public static boolean pda = false;
  // </editor-fold>

  public static void main(String[] args) {
    checkPDA();
    commandline(args);
    deProperties();
    lookAndFeel(huidigeLook);
    maakFrame();
    maakPanel();
    frame.setVisible(true);
  }

  public static void checkPDA() {
    if (System.getProperty("os.name").equals("Windows CE")) {
      pda = true;
    } else {
      pda = false;
    }
  }

  public static void maakPanel() {
    GamePanel p = new GamePanel();
    frame.add(p);
  }

  public static void drukaf(String t) {
    System.out.println(t);
  }

  public static void deProperties() {
    if (debug) {
      drukaf("properties...");
    }
    java.net.URL deURL = null;
    Properties props = new Properties();
    try {
      deURL = Main.class.getResource(propertiesFile);
      InputStream in = deURL.openStream();
      props.load(in);
      in.close();
    } catch (Exception ex) {
      System.out.println("Fout bij het lezen van de Properties-file\n(file: " + propertiesFile + ")\nFout: " + ex);
    }
    //Dingen doen
    if (debug) {
      drukaf("bezig met properties...");
    }
    //look and feel
    if (props.getProperty("lookAndFeel") != null && isInt(props.getProperty("lookAndFeel"))) {
      int foo = Integer.parseInt(props.getProperty("lookAndFeel"));
      if (foo < 0) {
        huidigeLook = 0;
      } else if (foo > 2) {
        huidigeLook = 2;
      } else {
        huidigeLook = foo;
      }
      if (Main.debug) {
        Main.drukaf("Main-Props: huidigeLook is nu: " + huidigeLook);
      }
    }
    //frameW
    if (props.getProperty("frameW") != null && isInt(props.getProperty("frameW"))) {
      int foo = Integer.parseInt(props.getProperty("frameW"));
      if (foo < 1) {
        frameW = 1;
      } else {
        frameW = foo;
      }
      if (Main.debug) {
        Main.drukaf("Main-Props: frameW is nu: " + frameW);
      }
    }
    //frameH
    if (props.getProperty("frameH") != null && isInt(props.getProperty("frameH"))) {
      int foo = Integer.parseInt(props.getProperty("frameH"));
      if (foo < 1) {
        frameH = 1;
      } else {
        frameH = foo;
      }
      if (Main.debug) {
        Main.drukaf("Main-Props: frameH is nu: " + frameH);
      }
    }
    //frameResizable
    if (props.getProperty("frameResizable") != null) {
      frameResizable = Boolean.parseBoolean(props.getProperty("frameResizable"));
    }
  }

  public static boolean isInt(String getal) {
    try {
      Integer.parseInt(getal);
    } catch (Exception e) {
      if (debug) {
        drukaf("isInt: " + getal + " is geen int getal !");
      }
      return false;
    }
    return true;
  }

  public static void lookAndFeel(int welke) {
    try {
      if (debug) {
        drukaf("lookAndFeel...");
      }
      if (welke == SYSTEM_LOOK) {
        javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        //System
        if (debug) {
          drukaf("lookAndFeel: SYSTEM_LOOK");
        }
      } else if (welke == NIMBUS_LOOK) {
        javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        //Nimbus
        if (debug) {
          drukaf("lookAndFeel: NIMBUS_LOOK");
        }
      } else if (welke == METAL_LOOK) {
        javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getCrossPlatformLookAndFeelClassName());
        //Metal
        if (debug) {
          drukaf("lookAndFeel: METAL_LOOK");
        }
      } else {
        drukaf("lookAndFeel: Fout: verkeerd argument (arg: " + welke + ")");
      }
    } catch (Exception ex) {
      drukaf("lookAndFeel: Fout: " + ex);
    }
  }

  public static void maakFrame() {
    frame = new JFrame();
    frame.setSize(frameW, frameH);
    frame.setResizable(frameResizable);
    frame.setLayout(new BorderLayout());
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.addWindowListener(new WindowAdapter() {

      @Override
      public void windowClosing(WindowEvent ev) {
        frameGesloten();
      }
    });
  }

  public static void frameGesloten() {
    //frame is gesloten
    if (debug) {
      drukaf("Venster is gesloten");
      drukaf("Programma wordt gestopt");
    }
    System.exit(0);
  }

  public static void commandline(String[] args) {

    drukaf("SnakeGame\nby FrankkieNL\n-h\t Commandline Opties\n--------------------");

    Getopt g = new Getopt("testprog", args, ":hd::D::pc");
    //
    int c;
    String arg;
    while ((c = g.getopt()) != -1) {
      switch (c) {

        case 'h': {
          printHelp();
          System.exit(0);
          break;
        }

        /*
        case 'd': {
        arg = g.getOptarg();
        if (arg == null) {
        dingFrameZichtbaar = true;
        } else {
        dingFrameZichtbaar = Boolean.parseBoolean(arg);
        }
        break;
        }
         */

        case 'D': {
          arg = g.getOptarg();
          if (arg == null) {
            debug = true;
          } else {
            debug = Boolean.getBoolean(arg);
          }
          drukaf("debug = " + debug);
          break;
        }

        case 'p': {
          pda = true;
          if (debug){
            drukaf("Force PDA-Mode");
          }
          break;
        }

        case 'c': {
          pda = false;
          if (debug){
            drukaf("Force Computer-Mode");
          }
          break;
        }

        default:
          drukaf("getopt() returned " + c + "\n");
          break;
      }
    }
  }

  public static void printHelp() {
    drukaf("Commandline Opties:");
    drukaf("-h\tLaat Deze Help Zien");
    //drukaf("-d [true|false]\tDingFrame laten zien\n(Alleen als dit programma het dingFrame gebruikt)");
    drukaf("-D [true|false]\tDebug-mode");
    drukaf("-p\tForce PDA-mode");
    drukaf("-c\tForce Computer-mode");
  }
}
